﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LotteryVote.Entity;
using LotteryVote.Data;

namespace LotteryVote.Logic.Limit
{
    /// <summary>
    /// 限制检查器
    /// </summary>
    internal class LimitChecker
    {
        private static object _lockHelper = new object();
        private static LimitChecker _checker;

        public static LimitChecker GetChecker()
        {
            if (_checker == null)
            {
                lock (_lockHelper)
                {
                    if (_checker == null)
                        _checker = new LimitChecker();
                }
            }
            return _checker;
        }

        private UpperLimiter _upperLimiter;
        internal UpperLimiter UpperLimiter
        {
            get
            {
                if (_upperLimiter == null)
                    _upperLimiter = new UpperLimiter();
                return _upperLimiter;
            }
            set
            {
                _upperLimiter = value;
            }
        }

        private UserLimiter _userLimiter;
        internal UserLimiter UserLimiter
        {
            get
            {
                if (_userLimiter == null)
                    _userLimiter = new UserLimiter();
                return _userLimiter;
            }
            set
            {
                _userLimiter = value;
            }
        }

        /// <summary>
        /// 错误信息
        /// </summary>
        public string ErrorMessage { get; private set; }
        /// <summary>
        /// 检查上限.
        /// </summary>
        /// <param name="order">The order.</param>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        public bool Check(BetOrder order, User user)
        {
            return UserLimiter.Inject(order) && UpperLimiter.Inject(order); ;
        }
        public void RollLimit(BetOrder order)
        {
            if (order.Status != BetStatus.Valid) return;
            UpperLimiter.RollOrderLimit(order);
        }
        public void UpdateLimit(DataBase tandemDB=null)
        {
            UpperLimiter.UpdateCacheUpperLimitToDB(tandemDB);
        }
    }
}
